package com.soccertgc.logging
{
	import com.soccertgc.core.DataObject;
	import com.soccertgc.utils.DateUtils;
	
	public class LogTargetParams extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON Property names
		protected const		JSON_PROPERTY_LEVEL:String					= "level";
		protected const		JSON_PROPERTY_TYPE:String					= "type";
		protected const		JSON_PROPERTY_SHOW_LEVEL:String				= "showLevel";
		protected const		JSON_PROPERTY_SHOW_DATE:String				= "showDate";
		protected const		JSON_PROPERTY_SHOW_TIME:String				= "showTime";
		protected const		JSON_PROPERTY_DATE_FORMAT:String			= "dateFormat";
		protected const		JSON_PROPERTY_TIME_FORMAT:String			= "timeFormat";
		protected const		JSON_PROPERTY_FILTERS:String				= "filters";
		
		// Default values
		protected const 	DEFAULT_LEVEL:LogLevel						= LogLevel.ALL;
		protected const 	DEFAULT_TARGET:LogTargetType				= LogTargetType.TRACE;
		protected const 	DEFAULT_SHOW_LEVEL:Boolean					= true;
		protected const 	DEFAULT_SHOW_CATEGORY:Boolean				= false;
		protected const 	DEFAULT_SHOW_DATE:Boolean					= true;
		protected const 	DEFAULT_SHOW_TIME:Boolean					= true;
		protected const		DEFAULT_DATE_FORMAT:String					= DateUtils.DEFAULT_DATE_FORMAT;
		protected const		DEFAULT_TIME_FORMAT:String					= DateUtils.DEFAULT_TIME_FORMAT;
		protected const		DEFAULT_FILTERS:Array						= ["com.soccertcg"];
		
		// Default configuration objects
		public static const	TRACE_TARGET_DATA:Object					= {level: LogLevel.ALL.name, 	type: LogTargetType.TRACE,		showLevel: true, showDate: false, showTime: true};
		public static const	REMOTE_TARGET_DATA:Object					= {level: LogLevel.WARN.name, 	type: LogTargetType.REMOTE, 	showLevel: true, showDate: false, showTime: true};
		public static const	TEXTAREA_TARGET_DATA:Object					= {level: LogLevel.ALL.name,	type: LogTargetType.TEXTAREA, 	showLevel: true, showDate: false, showTime: true};
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _level:LogLevel;
		private var _type:LogTargetType;
		private var _filters:Array;
		private var _showLevel:Boolean;
		private var _showDate:Boolean;
		private var _showTime:Boolean;
		private var _dateFormat:String;
		private var _timeFormat:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function LogTargetParams(data:Object = null)
		{
			// Clear data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get type():LogTargetType
		{
			return _type;
		}

		public function set type(value:LogTargetType):void
		{
			_type = value;
		}

		public function get level():LogLevel
		{
			return _level;
		}

		public function set level(value:LogLevel):void
		{
			_level = value;
		}
		
		public function get filters():Array
		{
			return _filters;
		}

		public function get showLevel():Boolean
		{
			return _showLevel;
		}

		public function set showLevel(value:Boolean):void
		{
			_showLevel = value;
		}

		public function get showDate():Boolean
		{
			return _showDate;
		}

		public function set showDate(value:Boolean):void
		{
			_showDate = value;
		}

		public function get showTime():Boolean
		{
			return _showTime;
		}

		public function set showTime(value:Boolean):void
		{
			_showTime = value;
		}
		
		public function get dateFormat():String
		{
			return _dateFormat;
		}
		
		public function set dateFormat(value:String):void
		{
			_dateFormat = value;
		}
		
		public function get timeFormat():String
		{
			return _timeFormat;
		}
		
		public function set timeFormat(value:String):void
		{
			_timeFormat = value;
		}

		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			// no validation
		}
		
		override protected function fill(data:Object):void
		{
			if (data.hasOwnProperty(JSON_PROPERTY_LEVEL)) 			_level 			= LogLevel.getByName(data[JSON_PROPERTY_LEVEL]);
			if (data.hasOwnProperty(JSON_PROPERTY_TYPE)) 			_type 			= data[JSON_PROPERTY_TYPE];
			if (data.hasOwnProperty(JSON_PROPERTY_SHOW_LEVEL))		_showLevel 		= data[JSON_PROPERTY_SHOW_LEVEL];
			if (data.hasOwnProperty(JSON_PROPERTY_SHOW_DATE))		_showDate 		= data[JSON_PROPERTY_SHOW_DATE];
			if (data.hasOwnProperty(JSON_PROPERTY_SHOW_TIME))		_showTime 		= data[JSON_PROPERTY_SHOW_TIME];
			// filters
			if (data.hasOwnProperty(JSON_PROPERTY_FILTERS))
			{
				var raw_filters:Array = data[JSON_PROPERTY_FILTERS];
				var filters:Vector.<String> = new Vector.<String>();
				// Create temporary log options list
				for (var x:int = 0; x < raw_filters.length; x++)
				{
					filters.push(raw_filters[x]);
				}
				// 
				_filters.splice(0, _filters.length); 
				for (var y:int = 0; y < raw_filters.length; y++)
				{
					_filters.push(filters[y]);
				}
			}
		}
		
		// Utils
		
		protected function equalsFilters(option:LogTargetParams):Boolean
		{
			if (_filters.length != option.filters.length) return false;
			for (var i:int=0; i < _filters.length; i++) if (_filters[i] != option.filters[i]) return false;
			return true;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function equals(logOption:LogTargetParams):Boolean
		{
			return	_type 			== logOption.type &&
					_level			== logOption.level &&
					_showLevel		== logOption.showLevel &&
					_showDate		== logOption.showDate &&
					_showTime		== logOption.showTime && 
					_dateFormat		== logOption.dateFormat && 
					_timeFormat		== logOption.timeFormat &&
					equalsFilters(logOption);
		}
		
		override public function clear():void
		{
			_level			= DEFAULT_LEVEL;
			_type			= DEFAULT_TARGET;
			_filters		= DEFAULT_FILTERS;
			_showLevel		= DEFAULT_SHOW_LEVEL;
			_showDate		= DEFAULT_SHOW_DATE;
			_showTime		= DEFAULT_SHOW_TIME;
			_dateFormat		= DEFAULT_DATE_FORMAT;
			_timeFormat		= DEFAULT_TIME_FORMAT;
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}

	}
}